package com.runa.rccprotocol.dto.hiwits;

import com.runa.protocol.dto.IResponseData;
import com.runa.protocol.util.ByteUtil;
import com.runa.protocol.util.DigitalUtil;
import com.runa.rccprotocol.dto.ConcentratorRequestData;

public class HiwitsRequestData extends ConcentratorRequestData {

    private byte[] requestData;

    public HiwitsRequestData(String address) {
        super(address);
    }

    public void readHeatMeterOfModbus(String serial){
        int no = Integer.parseInt(serial);
        requestData = new byte[]{(byte)no,0x03,0x00,0x00,0x00,0x16, (byte) 0x95, (byte) 0xD0};
        requestData[0] = (byte) no;
        int value = DigitalUtil.calcCrc16(requestData, 0, 6);
        requestData[6] = Integer.valueOf(value & 0xff).byteValue();
        value = value >> 8;
        requestData[7] = Integer.valueOf(value & 0xff).byteValue();
        setHaveResult(true);
    };

    public void readHeatMeter(String meter) {
        requestData = new byte[]{(byte) 0xfe, (byte) 0xfe, (byte) 0xfe, (byte) 0xfe,
                0x68, 0x20, 0, 0, 0, 0, 0, 0x55,0x55, 1, 3, 0x1f, (byte) 0x90, 0, 0, 0x16};
        ByteUtil.tenstringToBcd(requestData, meter, 6, false);
        requestData[18] = ByteUtil.sum(requestData, 4, 14);
        setHaveResult(true);
    }

    public void readHouseValveV20(String valve) {
        requestData = new byte[]{0x68, 0x42, 0, 0, 0, 0, 0, 0x55, 0x55, 1, 4, 0x1f, (byte) 0x90, 1, 0, 0, 0x16};
        ByteUtil.tenstringToBcd(requestData, valve, 2, false);
        requestData[15] = ByteUtil.sum(requestData, 0, 15);
        setHaveResult(true);
    }

    public void readHouseValveV21(String valve) {
        requestData = new byte[]{(byte) 0xfe, (byte) 0xfe, (byte) 0xfe, 0x68, 0x42, 0, 0, 0, 0, 0, 0x55, 0x55, 1, 5, (byte) 0x90, 0x1f, 1, 0, 0x50, 0, 0x16};
        ByteUtil.tenstringToBcd(requestData, valve, 5, false);
        requestData[19] = ByteUtil.sum(requestData, 3, 16);
        setHaveResult(true);
    }

    public void readBuildValve(String valve) {
        requestData = new byte[]{(byte) 0xfe, (byte) 0xfe, (byte) 0xfe, 0x68, 0x42, 0, 0, 0, 0, 1, 0x55, 0x55, 1, 3, (byte) 0x90, 0x20, 0, 0, 0x16};
        ByteUtil.tenstringToBcd(requestData, valve, 5, false);
        requestData[17] = ByteUtil.sum(requestData, 3, 14);
        setHaveResult(true);
    }

    public void houseValveOpenV20(String valve, int open, boolean lock) {
        requestData = new byte[]{0x68, 0x42, 0, 0, 0, 0, 0, 0x55, 0x55,
                4, 5, (byte) 0xa0, 0x23, 0, (byte) (lock ? 5 : 4), (byte) open, 0, 0x16};
        ByteUtil.tenstringToBcd(requestData, valve, 2, false);
        requestData[16] = ByteUtil.sum(requestData, 0, 16);
        setHaveResult(true);
    }

    public void houseValveOpenV21(String valve, int open) {
        //        requestData = newComm byte[]{0x68, 0x42, 0, 0, 0, 0, 0, 0x55, 0x55,
//                4, 7, (byte) 0xa0, 0x23, 1, 0x45, (byte) 0x90, 0x0a, (byte) (open/10), 0, 0x16};
        requestData = new byte[]{0x68, 0x42, 0, 0, 0, 0, 0, 0x55, 0x55,
                4, 7, (byte) 0x04, 0x16, 0, 0x64, 0, (byte) (open), 0x03, 0, 0x16};
        ByteUtil.tenstringToBcd(requestData, valve, 2, false);
        requestData[18] = ByteUtil.sum(requestData, 0, 18);
        setHaveResult(true);
    }

    public void houseValveTemp(String valve, float set, float up, float dw) {
        int Set = (int) (set * 10);
        int Up = (int) (up * 10);
        int Dw = (int) (dw * 10);
        requestData = new byte[]{0x68, 0x42, 0, 0, 0, 0, 0x55, 0x55, 0, 4, 13, 7, 0x16, 0,
                (byte) Set, (byte) (Set >>> 8), (byte) Up, (byte) (Up >>> 8), (byte) Dw, (byte) (Dw >>> 8),
                50, 0x0a, 0, 0, 0, 0x16};
        ByteUtil.tenstringToBcd(requestData, valve, 2, false);
        requestData[24] = ByteUtil.sum(requestData, 0, 24);
        setHaveResult(true);
    }

    public void buildValveOpenTemp(String valve, Integer open, Float temp) {
        if (open == null && temp == null) return;
        byte c = 0, d1, d2;
        if (open == null) {
            temp *= 100;
            c = 6;
            d1 = (byte) temp.intValue();
            d2 = (byte) (temp.intValue() >> 16);
        } else {
            d1 = 0;
            d2 = (byte) open.intValue();
        }
        requestData = new byte[]{(byte) 0xfe, (byte) 0xfe, (byte) 0xfe, 0x68, 0x42, 0, 0, 0, 0, 1, 0x55, 0x55, 4, 6, (byte) 0xa0, 0x26, 0, c, d2, d1, 0, 0x16};
        ByteUtil.tenstringToBcd(requestData, valve, 5, false);
        requestData[20] = ByteUtil.sum(requestData, 3, 17);
        setHaveResult(true);
    }

    @Override
    public boolean check(IResponseData data) {
        return true;
    }

    @Override
    public byte[] getData() {
        return requestData;
    }

    public static void main(String[] args) {
        HiwitsRequestData hiwitsRequestData = new HiwitsRequestData("00814774");
        hiwitsRequestData.readHeatMeterOfModbus("05132950");
//        System.out.println(ByteUtil.byteToHexString(requestData));
//        int i = Integer.parseInt("05132950");
//        byte i1 = (byte) i;
//        System.out.println(ByteUtil.byteToHexString(i1));
//        HiwitsRequestData requestData = new HiwitsRequestData("");
////        requestData.readHeatMeter("5138417");
////        System.out.println(ByteUtil.byteToHexString(requestData.getData()));
//
////        requestData.houseValveOpen("10375813", 88, true);
//        System.out.println(ByteUtil.byteToHexString(requestData.getData()));
//
//        requestData.readBuildValve("20100089");
//        System.out.println(ByteUtil.byteToHexString(requestData.getData()));
//
//        requestData.buildValveOpenTemp("20100089", 100, null);
//        System.out.println(ByteUtil.byteToHexString(requestData.getData()));
    }
}
